from turbogears import expose, validate, error_handler, flash
from formencode.api import Invalid
from mnemoply._renewed_lib.crud import CRUDController, TableCol
from mnemoply._renewed_model.field import Personality as model_Personality, EducationAgreement, StudentNew as model_Student
from mnemoply._renewed_utils import multiple_remove
from mnemoply._renewed_lib.validators import Schema, tg_validators
from mnemoply._renewed_constants import today, now
personality_columns = [
        TableCol(name='surname', caption=_('Surname')),
        TableCol(name='forename', caption=_('Forename')),
        TableCol(name='patronymic', caption=_('Patronymic')),
        TableCol(name='phones', caption=_('Phones')),
        TableCol(name='email', caption=_('e-mail'), hidden=True, size=9),
        TableCol(name='date_enter', caption=_('Entering date'), hidden=True, size=10)
   ]
personality_fields = [
        'accountID',        
        'surname',
        'forename',
        'patronymic',
        'email',
        ]

class PersonalityController(CRUDController):
    controller_name='Personality'
    model_object = model_Personality
    lookup_columns = personality_columns

    @expose(format="json")
    def find(self, obj=None, searchString=None):
        searchString=multiple_remove(searchString).replace('+7','8')
        return CRUDController.find(self,
                                   obj_condition= lambda obj: obj.title_full.lower(),
 #                                  lambda obj: "%s " % (obj.title_short),
                                   searchString =searchString
                                   )
    
    view_fields = add_fields = personality_fields
    edit_fields = personality_fields
    
    def before_update_edit(self, data, obj):
        agrs= EducationAgreement.selectBy(student=obj).count()
        if obj.account.id!=data['accountID']:
            if not agrs:
                agrs= EducationAgreement.selectBy(customer=obj).count()
            if agrs:
                data['accountID']=obj.account.id
                flash(_('Changes not saved!'))
        return CRUDController.before_update_edit(self, data, obj)


class CustomerController(PersonalityController):
    controller_name='Customer'
    model_object = model_Personality
    mysql_view_name = 'prod_v2.customer_lookup'
    lookup_columns = personality_columns
    view_fields = personality_columns
    def before_update(self, data, obj=None):
        data['isCustomer']=1
        return PersonalityController.before_update(self, data, obj)

class StudentSchema(Schema):
    birthday =tg_validators.DateValidator(latest_date=now.replace(year=now.year-3),
                                          earliest_date=now.replace(year=now.year-70))

class StudentController(PersonalityController):
    controller_name='Student_new'
    model_object = model_Student
    mysql_view_name = 'prod_v2.student_lookup'
    title = _("Student")
    lookup_columns = personality_columns+[
        TableCol(name='birthday', caption=_('Birthday'), size=4, hidden=True),
        TableCol(name='school_name', caption=_('School'), hidden=True, size=4),
        TableCol(name='school_form', caption=_('Form'), hidden=True, size=4),
        TableCol(name='academic_year_admission', caption=_('Year'), hidden=True, size=4),

        TableCol(name='surname_translit', caption=_('Surname translit'), size=10, hidden=True),
        TableCol(name='forename_translit', caption=_('Forename translit'), size=10, hidden=True),
        TableCol(name='patronymic_translit', caption=_('Patronymic translit'), size=10, hidden=True),
   ]

    edit_fields = personality_fields+[
        'birthday',
        'student_typeID',
        'school_name',
        'school_form',
        'academic_year_admission',
        ]

    view_fields = add_fields =personality_fields+[
        'birthday',
        'student_typeID',
        'school_name',
        'school_form',
        'academic_year_admission',
        ]

    chained_validators_edit = [StudentSchema()]

    def before_update(self, data, obj=None):
        data['isStudent']=1
        return PersonalityController.before_update(self, data, obj)


class EmployeeController(PersonalityController):
    controller_name='Employee'
    model_object = model_Personality
    mysql_view_name = 'prod_v2.employe_lookup'
    lookup_columns = personality_columns
    view_fields = personality_fields
    def before_update(self, data, obj=None):
        data['isEmployee']=1
        return PersonalityController.before_update(self, data, obj)
    

exposed_list = [StudentController(),
CustomerController(),
PersonalityController(),
EmployeeController(),]
